package com.jugheadzhou.mqtt.producer.controller;

import com.alibaba.fastjson.JSON;
import com.jugheadzhou.mqtt.producer.gateway.MqttGatewayComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author jugheadzhou
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {
    @Resource
    private MqttGatewayComponent mqttGatewayComponent;

    /**
     * 测试MQTT
     *
     * @return
     */
    @GetMapping("send")
    public ResponseEntity<?> send() {
        Map<String, Object> args = new HashMap<>();
        args.put("machineId", 83);
        args.put("status", RandomUtils.nextInt(0,2));
        mqttGatewayComponent.sendToMqtt(JSON.toJSONString(args), "test-topic/s2");
        return ResponseEntity.ok("发送成功");
    }

}
